package aspect;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import pojo.Customer;
@Component
@Aspect
public class DigestAspect {
	private final String p1="execution(* service.CustomerManager.regist(..))";
	private final String p2="execution(* service.CustomerManager.login(..))";
	private MessageDigest disgest;
	public DigestAspect(){
		try {
			disgest=MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	@Around(value=p1)
	public Object regist(ProceedingJoinPoint point) {
		Object o=null;
		Customer c=(Customer)point.getArgs()[0];
		c.setPassword(process(c.getPassword()));
		try {
			o=point.proceed(new Object[]{c});
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return o;
		
	}
	@Around(value=p2)
	public Object login(ProceedingJoinPoint point) {
		Object o=null;
		String s=(String)point.getArgs()[1];
		String ss=process(s);
		try {
			o=point.proceed(new Object[]{point.getArgs()[0],ss});
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return o;
	}
	
	private String process(String s){
		String ss=null;
		try {
			byte[] bs=disgest.digest(s.getBytes("ISO-8859-1"));
			ss=new String(bs,"ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return ss;
	}
}
